<?php
/**
 * Created by PhpStorm
 * user: MingLiang
 * Date：2021/9/9
 * Time: 2:13 下午
 */


namespace app\admin\controller\cost;


use app\admin\model\CostCompany;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use think\App;
use app\admin\model\CostBudget;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\annotation\ControllerAnnotation;
use app\admin\model\CostClassify;



/**
 * Class Budget
 * @package app\admin\controller\cost
 * @ControllerAnnotation(title="预算费用")
 */
class Budget extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new CostBudget();

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {

        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->withJoin(['classify','company'], 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin(['classify','company'], 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order(['budget_time' => 'desc'])
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'classify_id|一级分类'   => 'require',
                'budget_time|日期' => 'require',
                'company_id|城市划分' => 'require',
                'budget_price|预算费用' => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $post['budget_time'] = strtotime($post['budget_time']);
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $class_arr = CostClassify::field('title,id')->where('pid',0)->select()->toArray();
        $CostCompanyArr = CostCompany::field('id,company_name')->where('status',2)->select()->toArray();
        $this->assign('CostCompanyArr', $CostCompanyArr);
        $this->assign('class_arr', $class_arr);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {

        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'classify_id|一级分类'   => 'require',
                'budget_time|日期' => 'require',
                'budget_price|预算费用' => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $post['budget_time'] = strtotime($post['budget_time']);
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $class_arr = CostClassify::field('title,id')->where('pid',0)->select()->toArray();
        $row['budget_time'] = date('Y-m',$row['budget_time']);
        $CostCompanyArr = CostCompany::field('id,company_name')->where('status',2)->select()->toArray();
        $this->assign('CostCompanyArr', $CostCompanyArr);
        $this->assign('row', $row);
        $this->assign('class_arr', $class_arr);

        return $this->fetch();
    }

}